package com.hemaapp.yjnh.adapter;

import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.TextView;

import com.hemaapp.yjnh.BaseAdapter;
import com.hemaapp.yjnh.R;
import com.hemaapp.yjnh.activity.CunyouActivity;
import com.hemaapp.yjnh.activity.CmnBookMerchantListActivity;

/**村游县村列表的适配器
 * @author GuiyouZhang
 * 2015年9月15日
 */
public class CunyouAreaAdapter extends BaseAdapter {

	private CunyouActivity activity;
	private String[] towns;
	
	public CunyouAreaAdapter(CunyouActivity activity, String[] towns) {
		super(activity);
		this.activity = activity;
		this.towns = towns;
	}

	public void setTowns(String[] towns) {
		this.towns = towns;
	}
	
	@Override
	public int getCount() {
		int size = towns == null ? 0 : towns.length;
		return size == 0 ? 1 : towns.length;
	}
	
	@Override
    public boolean isEmpty() {
        int size = towns == null? 0:towns.length;
        return size == 0;
    }

	@Override
	public Object getItem(int position) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public long getItemId(int position) {
		// TODO Auto-generated method stub
		return 0;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		if (isEmpty()) 
			return getEmptyView(parent);
		ViewHolder holder;
        if (convertView == null) {
            convertView = LayoutInflater.from(mContext).inflate(
                    R.layout.item_cunyou_area, null);
            holder = new ViewHolder();
            findView(convertView, holder);
            convertView.setTag(R.id.TAG_VIEWHOLDER, holder);
        }
		setData(position, convertView);
		return convertView;
	}

	private class ViewHolder {
		private TextView tv_area;
	}
	
	private void findView(View convertView, ViewHolder holder) {
		holder.tv_area = (TextView)convertView.findViewById( R.id.tv_area );
	}
	
	private void setData(int position, View convertView) {
		ViewHolder holder = (ViewHolder) convertView.getTag(R.id.TAG_VIEWHOLDER);
		holder.tv_area.setText(towns[position]);
		convertView.setTag(towns[position]);
		convertView.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				activity.openDrawer(null);
				String town = (String) v.getTag();
				Intent itDistrict = new Intent(mContext, CmnBookMerchantListActivity.class);
				itDistrict.putExtra("keytype", "4");
				itDistrict.putExtra("keyid", town);
				itDistrict.putExtra("role", "");
                itDistrict.putExtra("title", town);
				mContext.startActivity(itDistrict);
			}
		});
	}
}
